/**
 * 
 */
package com.onewingu.gcs.server;

import java.security.Provider;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.google.gdata.util.common.util.Base64;

/**
 * @author er66
 *
 */
public class Encrypter {

	public static final String PASSWORD_KEY = "Freedom Real Estate Group LLC";
	public static final int ITERATION_COUNT = 20;
	public static final String CIPHER = "PBEWithHmacSHA1AndDESede";

	private static PBEKeySpec pbeKeySpec;
	private static PBEParameterSpec pbeParamSpec;
	private static SecretKeyFactory skf;
	private static SecretKeySpec sks;
	private static SecretKey sk;
	private static Cipher cipher;

	private static void initialize(int mode) throws Exception {
		try {
			//byte[] salt = {(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99};
			//char[] password = PASSWORD_KEY.toCharArray();

			//pbeParamSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
			//pbeKeySpec = new PBEKeySpec(password);
			//skf = SecretKeyFactory.getInstance("PBEWithHmacSHA1AndDESede");
			//sk = skf.generateSecret(pbeKeySpec);
			byte[] keyBytes = new byte[] {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05,
					0x04, 0x03, 0x02, 0x01, 0x00};
			sks = new SecretKeySpec(keyBytes, "AES");

			//cipher = Cipher.getInstance("AES/CBC/ISO10126Padding");
			cipher = Cipher.getInstance("AES");
			cipher.init(mode, sks);
		} catch(Exception e) {
			String msg = "";
			Provider[] providers = Security.getProviders();
			if(providers == null || providers.length == 0) {
				msg += "no crypto providers";
			} else {
				msg += "Providers:";
				for(Provider provider : providers) {
					msg += "\n" + provider.getName() + " --- " + provider.getInfo();
				}
			}
			Exception ex = new Exception(msg + "\n" + e.getMessage());
			throw ex;
		}
	}
	
	/*
	public static String encrypt(String clearText) throws Exception {
		initialize();
		//cipher.init(Cipher.ENCRYPT_MODE, sk, pbeParamSpec);
		cipher.init(Cipher.ENCRYPT_MODE, sks);
		
		byte[] clearBytes = clearText.getBytes();
		byte[] cipherBytes = cipher.doFinal(clearBytes);
		
		String cipherText = new String(cipherBytes);
		
		return cipherText;
	}
	
	public static String decrypt(String cipherText) throws Exception {
		initialize();
		//cipher.init(Cipher.DECRYPT_MODE, sk, pbeParamSpec);
		cipher.init(Cipher.DECRYPT_MODE, sks);
		
		byte[] cipherBytes = cipherText.getBytes();
		byte[] clearBytes = cipher.doFinal(cipherBytes);
		
		String clearText = new String(clearBytes);
		
		return clearText;
	}
	*/

	public static String encrypt(String clearText) throws Exception {
		initialize(Cipher.ENCRYPT_MODE);
		
		byte[] clearBytes = clearText.getBytes();
		byte[] cipherBytes = cipher.doFinal(clearBytes);

		String cipherText = Base64.encode(cipherBytes);
				
		return cipherText;
	}
	
	public static String decrypt(String cipherText) throws Exception {
		initialize(Cipher.DECRYPT_MODE);
		
		byte[] cipherBytes = Base64.decode(cipherText);
		byte[] clearBytes = cipher.doFinal(cipherBytes);
		
		String clearText = new String(clearBytes);
		
		return clearText;
	}
}
